<?php
	
	/**
	 * Join a MySQL Table near to the current devmod.
	 * 
	 * 17/01/2011 - class creation
	 * 
	 * @author bsuter
	 * @package devmod
	 */
	class Devmod_method_joinTable extends Devmod_methods_abstraction {

		protected $after_operation = array(array(
										'for_method' => 'export',
										'for_method_type' => 'list',
										'use' => 'addrow'
									), array(
										'for_method' => 'export',
										'for_method_type' => 'frontend_list',
										'use' => 'addrow'
									));
		
		/**
		 * #function params are observed by func_get_args#
		 * 
		 * @param $table [string] table name
		 * @param $args [array] array('where' => 'full where statement', 'onfield' => '
		 */
		public function set_config($args)
		{
			$this->update_config('jointable', array($args[0] => $args[1]));
		}

		public function addrow($args)
		{
			if(!isset($this->config[$this->type]['jointable'])) return $args;
			
			$joins = $this->config[$this->type]['jointable'];
			
			foreach($args as $args_key => $item)
			{
				foreach($joins as $name => $argue)
				{
					$_sql = $this->db->select($name, "*", isset($argue['where']) ? $this->helper->eval_vars($argue['where'], $item) : false, true, 'assoc');
					
					if(count($_sql) !== 1)
					{
						new Yomm_Error("joinTable throws error. sql_numrows for '{$this->db->last_query}' is: " . count($_sql));
					}
					
					$args[$args_key][$name] = $_sql[0];
				}
			}
			
			return $args;
		}
	}
